package client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

//Thread
class NewThread implements Runnable {
	Thread t;
        public static Timer ti;
        int t_to_r=10;
	NewThread()
	{
		t = new Thread(this);
		t.start();
	}
        public class Tick implements ActionListener {

            //JLabel label;
            Tick(){

            }
            public void actionPerformed(ActionEvent e) {
                
                int t = TCPClient.checkConnect();
                //System.out.println(t);
                if(t==0){
                    File f = new File("port.txt");
                    if(f.exists())
                        f.delete();
                    Main.frame.dispose();
                    try {
                        
                        if(Main.ch1.dataReceived){
                            Main.ch1.close();
                            ti.stop();
                        }
                        if(Main.ch2.dataReceived){
                            Main.ch2.close();
                            ti.stop();
                        }
                        if(Main.ch3.dataReceived){
                            Main.ch3.close();
                            ti.stop();
                        }
                        if(Main.ch4.dataReceived){
                            Main.ch4.close();
                            ti.stop();
                        }
                        Main.main(null);
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(Main.frame, ex.getMessage());
                    } catch (ClassNotFoundException ex) {
                        JOptionPane.showMessageDialog(Main.frame, ex.getMessage());
                    }
                }
                //Main.getnew.setText("You have : "+t+" new email");
            }
        }
	public void run()
	{
		try
		{
                    //int i = 0;
                    Thread.sleep(200);
                    //i = TCPClient.checkConnect();
                    ti = new Timer(1000,new Tick());
                    ti.start();
		}
		catch (InterruptedException e)
		{
			JOptionPane.showMessageDialog(Main.frame, "Error");
		}

	}
}
////End thread
public class Main extends JPanel implements ActionListener {
    protected JButton b1, b2, b3, b4, b5;
    public static Channel1 ch1;
    public static Channel2 ch2;
    public static Channel3 ch3;
    public static Channel4 ch4;
    static JFrame frame,framelogin,frameSetting,fcontentemail;


   public static void login(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        framelogin = new JFrame("Login");
        framelogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Login n = new Login();
        framelogin.setContentPane(n);
        framelogin.pack();
        framelogin.setVisible(true);

    }
    public static void setting(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        frameSetting = new JFrame("Setting");
        frameSetting.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Setting n = new Setting();
        frameSetting.setContentPane(n);
        frameSetting.pack();
        frameSetting.setVisible(true);
    }
    
    public Main(){
        try {
            
            String[] str = TCPClient.readF("port.txt");

            String m1[] = new String[2];
            m1[0] = str[0]+"/"+str[1]+"/1";
            m1[1] = str[0]+"/"+(Integer.parseInt(str[1])+2)+"/1";
            ch1 = new Channel1(m1);

            String m2[] = new String[2];
            m2[0] = str[0]+"/"+str[2]+"/1";
            m2[1] = str[0]+"/"+(Integer.parseInt(str[2])+2)+"/1";
            ch2 = new Channel2(m2);

            String m3[] = new String[2];
            m3[0] = str[0]+"/"+str[3]+"/1";
            m3[1] = str[0]+"/"+(Integer.parseInt(str[3])+2)+"/1";
            ch3 = new Channel3(m3);

            String m4[] = new String[2];
            m4[0] = str[0]+"/"+str[4]+"/1";
            m4[1] = str[0]+"/"+(Integer.parseInt(str[4])+2)+"/1";
            ch4 = new Channel4(m4);


            b1 = new JButton("Channel1");
            b1.setActionCommand("channel1");

            b2 = new JButton("Channel2");
            b2.setActionCommand("channel2");

            b3 = new JButton("Channel3");
            b3.setActionCommand("channel3");

            b4 = new JButton("Channel4");
            b4.setActionCommand("channel4");


            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);

            add(b1);
            add(b2);
            add(b3);
            add(b4);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Not read file");
            System.exit(0);
        }
    }
    
    public static void createAndShowGUI() {
        NewThread thread = new NewThread();
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                    NewThread.ti.stop();
                    TCPClient.exitConnect();
            }
        });
        Main newContentPane = new Main();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        //frame.pack();
        //frame.setVisible(true);
    }
    
    public static void main(String[] args) throws IOException, ClassNotFoundException {
       // createAndShowGUI();
        File f = new File("FileConnect.txt");
        if(f.exists()){
            if(TCPClient.checkConnect()==1){
                login();
            }
            else
                setting();
        }
        else
            setting();
        
    }

    public void actionPerformed(ActionEvent e) {
       String command = e.getActionCommand();
       if(command.equals("channel1")){
           if(ch2.dataReceived){
               ch2.close();
           }
           if(ch3.dataReceived){
               ch3.close();
           }
           if(ch4.dataReceived){
               ch4.close();
           }

            if (!ch1.initialize()) {
                JOptionPane.showMessageDialog(this, "Can not connect");
            }

       }
       if(command.equals("channel2")){
           if(ch1.dataReceived){
               ch1.close();             
           }
           if(ch3.dataReceived){
               ch3.close();
           }
           if(ch4.dataReceived){
               ch4.close();
           }

            if (!ch2.initialize()) {
                JOptionPane.showMessageDialog(this, "Can not connect");
            }
       }
       if(command.equals("channel3")){
           if(ch1.dataReceived){
               ch1.close();
           }
           if(ch2.dataReceived){
               ch2.close();
           }
           if(ch4.dataReceived){
               ch4.close();
           }

            if (!ch3.initialize()) {
                JOptionPane.showMessageDialog(this, "Can not connect");
            }
       }
       if(command.equals("channel4")){
           if(ch1.dataReceived){
               ch1.close();
           }
           if(ch3.dataReceived){
               ch3.close();
           }
           if(ch2.dataReceived){
               ch2.close();
           }

            if (!ch4.initialize()) {
                JOptionPane.showMessageDialog(this, "Can not connect");
            }
       }
    }
}